package org.jeecg.modules.xl.order.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.dromara.pdf.pdfbox.core.base.*;
import org.dromara.pdf.pdfbox.core.component.*;
import org.dromara.pdf.pdfbox.core.enums.BarcodeType;
import org.dromara.pdf.pdfbox.core.component.Image;
import org.dromara.pdf.pdfbox.core.enums.ContentMode;
import org.dromara.pdf.pdfbox.core.enums.HorizontalAlignment;
import org.dromara.pdf.pdfbox.core.enums.VerticalAlignment;
import org.dromara.pdf.pdfbox.handler.PdfHandler;
import org.dromara.pdf.pdfbox.support.Constants;
import org.jeecg.modules.xl.order.dto.OrderDetailNew;
import org.jeecg.modules.xl.order.entity.XlCustomerOrde;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class PdfHandle {
    public static void handle(XlCustomerOrde order, List<OrderDetailNew> xlCustomerOrderDetailList, HttpServletResponse response ) throws IOException {
        // 定义pdf输出路径
        String outputPath = ("/Users/deco/Downloads/temp/hello-world-"+RandomUtil.randomNumbers(6)+".pdf");
        PdfHandler.disableScanSystemFonts();
        // 创建文档
        Document document = PdfHandler.getDocumentHandler().create();
        // 设置字体
        document.setFontName("微软雅黑");
        // 创建页面
        Page page = new Page(document, PageSize.A4);
        page.setBackgroundColor(new Color(255, 255, 255));
        // 页眉
       // header(document.getCurrentPage());
        // 页脚
        //footer(document.getCurrentPage(), document.getTotalPageNumber());
        // 标题
        //title(page);

        Textarea textarea = new Textarea(page);
        // 设置文本
        textarea.setText("好特麦商联通-"+"-对账单");
        // 字体
        textarea.setFontSize(21);
        // 字体色号
        textarea.setFontColor(new Color(0, 13, 52));
        // 居中
        textarea.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // Y坐标
        textarea.setRelativeBeginY(25F);
        // 渲染文本
        textarea.render();


        // 表格
        //table(  document,page,order,xlCustomerOrderDetailList);
        Table table = new Table(page);
        table.setMarginTop(20);/*
       table.setMarginLeft(28f);
       table.setMarginRight(28f);*/
        float width = page.getWidth() / 12;

        float height = 200F;
        String[] cellHea = { "行号", "订单日期" ,"SKU/UPC","商品名称","单位","订单数量","售后数量","实发数量","订货单价","应付金额", "售后备注"};
        table.setCellWidths(width,width,width,width,width,width,width,width,width,width,width);
        // 设置列宽（4列）
        //table.setCellWidths(100, 170, 100, 170);
        // 设置显示边框
        table.setIsBorder(true);

        //第一行 单号 + 收货人
        TableRow firtRow = new TableRow(table);

        firtRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
        firtRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
        firtRow.setHeight(40);

        TableCell orderLabelCell = createTextCell("单号",table,firtRow,1);
        orderLabelCell.setColspan(1);
        // firtRow.addCells(orderLabelCell);

        TableCell orderValueCell = createTextCell(order.getOrderCode(),table,firtRow,2);
        orderValueCell.setColspan(2);
        //firtRow.addCells(orderValueCell);

        TableCell receiptLabelCell = createTextCell("收货人",table,firtRow,1);
        receiptLabelCell.setColspan(1);
        //firtRow.addCells(receiptLabelCell);

        TableCell receiptValueCell = createTextCell(order.getReceiptName(),table,firtRow,2);
        receiptValueCell.setColspan(3);

        firtRow.addCells(orderLabelCell,null,orderValueCell,null,null,receiptLabelCell,null,receiptValueCell,null,null,null);

        table.addRows(firtRow);
        // 添加行

        //第二行 单号 + 收货人
        TableRow secondRow = new TableRow(table);
        secondRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
        secondRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
        secondRow.setHeight(40);
        TableCell customerLabelCell = createTextCell("客户名",table,firtRow,1);
        customerLabelCell.setColspan(1);

        TableCell customerValueCell = createTextCell(order.getStoreName(),table,firtRow,2);
        customerValueCell.setColspan(2);

        TableCell receiptPhoneLabelCell = createTextCell("收货人电话",table,firtRow,1);
        receiptPhoneLabelCell.setColspan(1);
        //firtRow.addCells(receiptLabelCell);

        TableCell receiptPhoneValueCell = createTextCell(order.getReceiptPhone(),table,firtRow,2);
        receiptPhoneValueCell.setColspan(3);
        secondRow.addCells(customerLabelCell,null,customerValueCell,null,null,receiptPhoneLabelCell,null,receiptPhoneValueCell,null,null,null);

        // 添加行
        table.addRows(secondRow);


        //第三行 收货地址
        TableRow thirdRow = new TableRow(table);
        thirdRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
        thirdRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
        thirdRow.setHeight(40);

        TableCell receiptAddrLabelCell = createTextCell("收货地址",table,firtRow,1);
        receiptAddrLabelCell.setColspan(1);

        TableCell receiptvalueLabelCell = createTextCell(order.getReceiptAddress(),table,firtRow,2);
        receiptvalueLabelCell.setColspan(8);

        thirdRow.addCells(receiptAddrLabelCell,null,receiptvalueLabelCell,null,null,null,null,null,null,null,null);

        table.addRows(thirdRow);

       detailTable(  document,page,order,xlCustomerOrderDetailList);
        // 运行概述
        //running(page);
        // 运行概述表格


        // 页脚
        //footer(document.getCurrentPage(), document.getTotalPageNumber());
        // 添加页面

//        Image image = new Image(page);
//        image.setWidth(200);
//        image.setHeight(200);
//        image.setImage(Paths.get(".\\pay\\1.jpg").toFile());
//        image.render();
       // detailTable(page);
        page.setMargin(50F);
        document.appendPage(page);
        // 保存并关闭文档
        //document.saveAndClose(outputPath);
        document.saveAndClose(response.getOutputStream());
    }

    private static void title(Page page) {
        // 创建文本域
        Textarea textarea = new Textarea(page);
        // 设置文本
        textarea.setText("好特麦商联通-"+"-对账单");
        // 字体
        textarea.setFontSize(21);
        // 字体色号
        textarea.setFontColor(new Color(0, 13, 52));
        // 居中
        textarea.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // Y坐标
        textarea.setRelativeBeginY(25F);
        // 渲染文本
        textarea.render();
    }
    private static void table(Document document,Page page,XlCustomerOrde order,List<OrderDetailNew> xlCustomerOrderDetailList) {
        // 创建表格
        Table table = new Table(page);
        table.setMarginTop(20);/*
       table.setMarginLeft(28f);
       table.setMarginRight(28f);*/
        float width = page.getWidth() / 12;

        float height = 200F;
        String[] cellHea = { "行号", "订单日期" ,"SKU/UPC","商品名称","单位","订单数量","售后数量","实发数量","订货单价","应付金额", "售后备注"};
        table.setCellWidths(width,width,width,width,width,width,width,width,width,width,width);
        // 设置列宽（4列）
        //table.setCellWidths(100, 170, 100, 170);
        // 设置显示边框
        table.setIsBorder(true);

        //第一行 单号 + 收货人
        TableRow firtRow = new TableRow(table);

        firtRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
        firtRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
        firtRow.setHeight(40);

        TableCell orderLabelCell = createTextCell("单号",table,firtRow,1);
        orderLabelCell.setColspan(1);
       // firtRow.addCells(orderLabelCell);

        TableCell orderValueCell = createTextCell(order.getOrderCode(),table,firtRow,2);
        orderValueCell.setColspan(2);
        //firtRow.addCells(orderValueCell);

        TableCell receiptLabelCell = createTextCell("收货人",table,firtRow,1);
        receiptLabelCell.setColspan(1);
        //firtRow.addCells(receiptLabelCell);

        TableCell receiptValueCell = createTextCell(order.getReceiptName(),table,firtRow,2);
        receiptValueCell.setColspan(3);

        firtRow.addCells(orderLabelCell,null,orderValueCell,null,null,receiptLabelCell,null,receiptValueCell,null,null,null);

        table.addRows(firtRow);
        // 添加行

        //第二行 单号 + 收货人
        TableRow secondRow = new TableRow(table);
        secondRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
        secondRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
        secondRow.setHeight(40);
        TableCell customerLabelCell = createTextCell("客户名",table,firtRow,1);
        customerLabelCell.setColspan(1);

        TableCell customerValueCell = createTextCell(order.getStoreName(),table,firtRow,2);
        customerValueCell.setColspan(2);

        TableCell receiptPhoneLabelCell = createTextCell("收货人电话",table,firtRow,1);
        receiptPhoneLabelCell.setColspan(1);
        //firtRow.addCells(receiptLabelCell);

        TableCell receiptPhoneValueCell = createTextCell(order.getReceiptPhone(),table,firtRow,2);
        receiptPhoneValueCell.setColspan(3);
        secondRow.addCells(customerLabelCell,null,customerValueCell,null,null,receiptPhoneLabelCell,null,receiptPhoneValueCell,null,null,null);

        // 添加行
        table.addRows(secondRow);


        //第三行 收货地址
        TableRow thirdRow = new TableRow(table);
        thirdRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
        thirdRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
        thirdRow.setHeight(40);

        TableCell receiptAddrLabelCell = createTextCell("收货地址",table,firtRow,1);
        receiptAddrLabelCell.setColspan(1);

        TableCell receiptvalueLabelCell = createTextCell(order.getReceiptAddress(),table,firtRow,2);
        receiptvalueLabelCell.setColspan(8);

        thirdRow.addCells(receiptAddrLabelCell,null,receiptvalueLabelCell,null,null,null,null,null,null,null,null);

        table.addRows(thirdRow);
//
//        TableRow deatailHeaderRow = new TableRow(table);
//        deatailHeaderRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
//        deatailHeaderRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
//        deatailHeaderRow.setHeight(40);
//        deatailHeaderRow.addCells(createTextCell("行号",table,deatailHeaderRow,1));
//        deatailHeaderRow.addCells(createTextCell("订单日期",table,deatailHeaderRow,1));
//        deatailHeaderRow.addCells(createTextCell("SKU/UPC编码",table,deatailHeaderRow,2));
//        deatailHeaderRow.addCells(createTextCell("商品名称",table,deatailHeaderRow,1));
//        deatailHeaderRow.addCells(createTextCell("单位",table,deatailHeaderRow,2));
//        deatailHeaderRow.addCells(createTextCell("订单数量",table,deatailHeaderRow,1));
//        deatailHeaderRow.addCells(createTextCell("售后数量",table,deatailHeaderRow,2));
//        deatailHeaderRow.addCells(createTextCell("实发数量",table,deatailHeaderRow,1));
//        deatailHeaderRow.addCells(createTextCell("订货单价",table,deatailHeaderRow,2));
//        deatailHeaderRow.addCells(createTextCell("订货应付金额",table,deatailHeaderRow,1));
//        deatailHeaderRow.addCells(createTextCell("备注",table,deatailHeaderRow,2));
//        // 添加行
//        table.addRows(deatailHeaderRow);
//        //添加明细
//        for (int i = 0; i < xlCustomerOrderDetailList.size(); i++) {
//            TableRow deatailDataRow = new TableRow(table);
//            OrderDetailNew temp = xlCustomerOrderDetailList.get(i);
//            deatailDataRow.setHeight(40);
//
//            deatailDataRow.addCells(createTextCell(String.valueOf(i+1),table,deatailHeaderRow,1));
//            deatailDataRow.addCells(createTextCell(DateUtil.format(order.getCreateTime(),"MM-dd"),table,deatailHeaderRow,2));
//            deatailDataRow.addCells(createTextCell(temp.getUpcCode(),table,deatailHeaderRow,1));
//            deatailDataRow.addCells(createTextCell(temp.getProductName(),table,deatailHeaderRow,1));
//            deatailDataRow.addCells(createTextCell("单位",table,deatailHeaderRow,2));
//            deatailDataRow.addCells(createTextCell(temp.getOrderQuantity().toString(),table,deatailHeaderRow,1));
//            deatailDataRow.addCells(createTextCell(temp.getAftersaleQuantity().toString(),table,deatailHeaderRow,2));
//            deatailDataRow.addCells(createTextCell(temp.getRealQuantity().toString(),table,deatailHeaderRow,1));
//            deatailDataRow.addCells(createTextCell(temp.getProductPrice().toString(),table,deatailHeaderRow,2));
//            deatailDataRow.addCells(createTextCell(temp.getOrderAmount().toString(),table,deatailHeaderRow,1));
//            deatailDataRow.addCells(createTextCell("售后备注",table,deatailHeaderRow,2));
//            table.addRows(deatailDataRow);
//        }
        // 绘制
        table.render();
    }
    private static void detailTable(Document document,Page page,XlCustomerOrde order,List<OrderDetailNew> xlCustomerOrderDetailList) {
        // 创建表格
        Table table = new Table(page);
        table.setMarginTop(20);/*
       table.setMarginLeft(28f);
       table.setMarginRight(28f);*/
        float width = page.getWidth() / 11;

        float height = 200F;
        String[] cellHea = { "行号", "订单日期" ,"SKU/UPC","商品名称","单位","订单数量","售后数量","实发数量","订货单价","应付金额", "售后备注"};
        table.setCellWidths(width,width,width,width,width,width,width,width,width,width,width);
        // 设置列宽（4列）
        //table.setCellWidths(100, 170, 100, 170);
        // 设置显示边框
        table.setIsBorder(true);

//        //第一行 单号 + 收货人
//        TableRow firtRow = new TableRow(table);
//
//        firtRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
//        firtRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
//        firtRow.setHeight(40);
//
//        TableCell orderLabelCell = createTextCell("单号",table,firtRow,1);
//        orderLabelCell.setColspan(1);
//        // firtRow.addCells(orderLabelCell);
//
//        TableCell orderValueCell = createTextCell(order.getOrderCode(),table,firtRow,2);
//        orderValueCell.setColspan(2);
//        //firtRow.addCells(orderValueCell);
//
//        TableCell receiptLabelCell = createTextCell("收货人",table,firtRow,1);
//        receiptLabelCell.setColspan(1);
//        //firtRow.addCells(receiptLabelCell);
//
//        TableCell receiptValueCell = createTextCell(order.getReceiptName(),table,firtRow,2);
//        receiptValueCell.setColspan(3);
//
//        firtRow.addCells(orderLabelCell,null,orderValueCell,null,null,receiptLabelCell,null,receiptValueCell,null,null,null);
//
//        table.addRows(firtRow);
//        // 添加行
//
//        //第二行 单号 + 收货人
//        TableRow secondRow = new TableRow(table);
//        secondRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
//        secondRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
//        secondRow.setHeight(40);
//        TableCell customerLabelCell = createTextCell("客户名",table,firtRow,1);
//        customerLabelCell.setColspan(1);
//
//        TableCell customerValueCell = createTextCell(order.getStoreName(),table,firtRow,2);
//        customerValueCell.setColspan(2);
//
//        TableCell receiptPhoneLabelCell = createTextCell("收货人电话",table,firtRow,1);
//        receiptPhoneLabelCell.setColspan(1);
//        //firtRow.addCells(receiptLabelCell);
//
//        TableCell receiptPhoneValueCell = createTextCell(order.getReceiptPhone(),table,firtRow,2);
//        receiptPhoneValueCell.setColspan(3);
//        secondRow.addCells(customerLabelCell,null,customerValueCell,null,null,receiptPhoneLabelCell,null,receiptPhoneValueCell,null,null,null);
//
//        // 添加行
//        table.addRows(secondRow);
//
//
//        //第三行 收货地址
//        TableRow thirdRow = new TableRow(table);
//        thirdRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
//        thirdRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
//        thirdRow.setHeight(40);
//
//        TableCell receiptAddrLabelCell = createTextCell("收货地址",table,firtRow,1);
//        receiptAddrLabelCell.setColspan(1);
//
//        TableCell receiptvalueLabelCell = createTextCell(order.getReceiptAddress(),table,firtRow,2);
//        receiptvalueLabelCell.setColspan(8);
//
//        thirdRow.addCells(receiptAddrLabelCell,null,receiptvalueLabelCell,null,null,null,null,null,null,null,null);
//
//        table.addRows(thirdRow);

        TableRow deatailHeaderRow = new TableRow(table);
        deatailHeaderRow.setContentHorizontalAlignment(HorizontalAlignment.CENTER);
        deatailHeaderRow.setContentVerticalAlignment(VerticalAlignment.CENTER);
        deatailHeaderRow.setHeight(40);
        deatailHeaderRow.addCells(createTextCell("行号",table,deatailHeaderRow,1));
        deatailHeaderRow.addCells(createTextCell("订单日期",table,deatailHeaderRow,1));
        deatailHeaderRow.addCells(createTextCell("SKU/UPC编码",table,deatailHeaderRow,2));
        deatailHeaderRow.addCells(createTextCell("商品名称",table,deatailHeaderRow,1));
        deatailHeaderRow.addCells(createTextCell("单位",table,deatailHeaderRow,2));
        deatailHeaderRow.addCells(createTextCell("订单数量",table,deatailHeaderRow,1));
        deatailHeaderRow.addCells(createTextCell("售后数量",table,deatailHeaderRow,2));
        deatailHeaderRow.addCells(createTextCell("实发数量",table,deatailHeaderRow,1));
        deatailHeaderRow.addCells(createTextCell("订货单价",table,deatailHeaderRow,2));
        deatailHeaderRow.addCells(createTextCell("订货应付金额",table,deatailHeaderRow,1));
        deatailHeaderRow.addCells(createTextCell("备注",table,deatailHeaderRow,2));
        // 添加行
        table.addRows(deatailHeaderRow);
        //添加明细
        for (int i = 0; i < xlCustomerOrderDetailList.size(); i++) {
            TableRow deatailDataRow = new TableRow(table);
            OrderDetailNew temp = xlCustomerOrderDetailList.get(i);
            deatailDataRow.setHeight(40);

            deatailDataRow.addCells(createTextCell(String.valueOf(i+1),table,deatailHeaderRow,1));
            deatailDataRow.addCells(createTextCell(DateUtil.format(order.getCreateTime(),"MM-dd"),table,deatailHeaderRow,2));
            deatailDataRow.addCells(createTextCell(temp.getUpcCode(),table,deatailHeaderRow,1));
            deatailDataRow.addCells(createTextCell(temp.getProductName(),table,deatailHeaderRow,1));
            deatailDataRow.addCells(createTextCell("单位",table,deatailHeaderRow,2));
            deatailDataRow.addCells(createTextCell(temp.getOrderQuantity().toString(),table,deatailHeaderRow,1));
            deatailDataRow.addCells(createTextCell(temp.getAftersaleQuantity().toString(),table,deatailHeaderRow,2));
            deatailDataRow.addCells(createTextCell(temp.getRealQuantity().toString(),table,deatailHeaderRow,1));
            deatailDataRow.addCells(createTextCell(temp.getProductPrice().toString(),table,deatailHeaderRow,2));
            deatailDataRow.addCells(createTextCell(temp.getOrderAmount().toString(),table,deatailHeaderRow,1));
            deatailDataRow.addCells(createTextCell("售后备注",table,deatailHeaderRow,2));
            table.addRows(deatailDataRow);
        }
        // 绘制
        table.render();
    }

    private static TableCell createTextCell(String text, Table table,  TableRow row ,int cellType){
        Textarea textarea = new Textarea(table.getPage() );
        textarea.setFontSize(10);
//        textarea.setFontColor(new Color(255, 255, 255));
        textarea.setFontColor(Color.BLACK);
        // 设置文本
        textarea.setText(text);
        // 创建单元格
        TableCell cell = new TableCell(row);
        if(2 == cellType){
            cell.setBackgroundColor(new Color(247, 247, 247));
        }
        // 添加组件
        cell.addComponents(textarea);
        return cell;
    }

}
